<?php
/**
 * @file
 * @package mileage
 * @version $Id$
**/

if (!defined('XOOPS_ROOT_PATH')) {
	exit;
}

require_once MILEAGE_TRUST_PATH . '/class/AbstractFilterForm.class.php';

define('MILEAGE_MILE_SORT_KEY_MILE_ID',      1);
define('MILEAGE_MILE_SORT_KEY_NAME',         2);
define('MILEAGE_MILE_SORT_KEY_POINT',        3);
define('MILEAGE_MILE_SORT_KEY_UID',          4);
define('MILEAGE_MILE_SORT_KEY_REG_UNIXTIME', 5);
define('MILEAGE_MILE_SORT_KEY_URL',          6);
define('MILEAGE_MILE_SORT_KEY_SUBURL',       7);
define('MILEAGE_MILE_SORT_KEY_INTERVAL',     8);
define('MILEAGE_MILE_SORT_KEY_GROUP',        9);

define('MILEAGE_MILE_SORT_KEY_DEFAULT', MILEAGE_MILE_SORT_KEY_REG_UNIXTIME);

/**
 * Mileage_Admin_MileFilterForm
**/
class Mileage_Admin_MileFilterForm extends Mileage_AbstractFilterForm
{
	/**
	 * @var  string[]
	 * 
	 * @public
	**/
	var $mSortKeys = array(
		MILEAGE_MILE_SORT_KEY_MILE_ID      => 'mile_id',
		MILEAGE_MILE_SORT_KEY_NAME         => 'name',
		MILEAGE_MILE_SORT_KEY_POINT        => 'point',
		MILEAGE_MILE_SORT_KEY_UID          => 'uid',
		MILEAGE_MILE_SORT_KEY_REG_UNIXTIME => 'reg_unixtime',
		MILEAGE_MILE_SORT_KEY_URL          => 'url',
		MILEAGE_MILE_SORT_KEY_SUBURL       => 'suburl',
		MILEAGE_MILE_SORT_KEY_INTERVAL     => 'interval',
		MILEAGE_MILE_SORT_KEY_GROUP        => 'group',
	);

	/**
	 * getDefaultSortKey
	 * 
	 * @param   void
	 * 
	 * @return  void
	**/
	public function getDefaultSortKey()
	{
		return MILEAGE_MILE_SORT_KEY_DEFAULT * -1;
	}

	/**
	 * fetch
	 * 
	 * @param   void
	 * 
	 * @return  void
	**/
	public function fetch()
	{
		parent::fetch();

		$root =& XCube_Root::getSingleton();

		if (($value = $root->mContext->mRequest->getRequest('mile_id')) !== null) {
			$this->mNavi->addExtra('mile_id', $value);
			$this->_mCriteria->add(new Criteria('meil_id', $value));
		}

		if (($value = $root->mContext->mRequest->getRequest('name')) !== null) {
			$this->mNavi->addExtra('name', $value);
			$this->_mCriteria->add(new Criteria('name', $value));
		}

		if (($value = $root->mContext->mRequest->getRequest('point')) !== null) {
			$this->mNavi->addExtra('point', $value);
			$this->_mCriteria->add(new Criteria('point', $value));
		}

		if (($value = $root->mContext->mRequest->getRequest('uid')) !== null) {
			$this->mNavi->addExtra('uid', $value);
			$this->_mCriteria->add(new Criteria('uid', $value));
		}

		if (($value = $root->mContext->mRequest->getRequest('reg_unixtime')) !== null) {
			$this->mNavi->addExtra('reg_unixtime', $value);
			$this->_mCriteria->add(new Criteria('reg_unixtime', $value));
		}

		if (($value = $root->mContext->mRequest->getRequest('url')) !== null) {
			$this->mNavi->addExtra('url', $value);
			$this->_mCriteria->add(new Criteria('url', $value));
		}

		if (($value = $root->mContext->mRequest->getRequest('suburl')) !== null) {
			$this->mNavi->addExtra('suburl', $value);
			$this->_mCriteria->add(new Criteria('suburl', $value));
		}

		if (($value = $root->mContext->mRequest->getRequest('interval')) !== null) {
			$this->mNavi->addExtra('interval', $value);
			$this->_mCriteria->add(new Criteria('interval', $value));
		}

		if (($value = $root->mContext->mRequest->getRequest('group')) !== null) {
			$this->mNavi->addExtra('group', $value);
			$this->_mCriteria->add(new Criteria('`group`', '%[' . $value . ']%', 'LIKE'));
		}

		$this->_mCriteria->addSort($this->getSort(), $this->getOrder());
	}
}
